using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class GUIHealthBar : MonoBehaviour {
	//public GUIStyle Healthbar;
	public bool 		debug 					= false;
	
	private bool display = true;
	
	//Textures
	public Texture2D 	HealthBarTexture;	
	public Texture2D 	HealthBarBorderTexture;
	public Texture2D	HealthBarBackground;
	public Texture2D 	healthSquareBorderTexture;

	public Vector2 		HealthBarPosition 			= new Vector2(0,0);

	// Health Bar Border Thickness
	public float 		HealthBarBorderSizeLeft 	= 100;
	public float 		HealthBarBorderSizeRight	= 100;
	public float 		HealthBarBorderSizeUp 		= 100;
	public float 		HealthBarBorderSizeDown 	= 50;
	
	private float		maxHealth = 100;
	private float 		Health;
	private Vector2 	HealthBarSize;
	
	public float paddingUp 		= 5;
	public float paddingDown 	= 5;
	
	public float healthSquareValue = 20;
	
	private bool displayBackGround 	= true;
	private bool displayDividers	= true;
	private bool displayHealthBar	= true;
	private bool displayBorder		= true;
		
	void Start() {
		if (HealthBarTexture != null && HealthBarBorderTexture != null && HealthBarBackground != null && healthSquareBorderTexture != null) {
			Health = maxHealth;
			HealthBarSize = new Vector2(HealthBarBorderTexture.width,HealthBarBorderTexture.height);

		}
	}
	
	
	
	
	void OnGUI () {

		if (display){
		// HEALTHBAR----------------------------------------------------------------------------------------------------------------
		if (HealthBarTexture 			!= null && 
			HealthBarBorderTexture 		!= null &&
			healthSquareBorderTexture	!= null) {
			
			// Health Bar Area
			GUI.BeginGroup(new Rect(	HealthBarPosition.x,
										HealthBarPosition.y,
										HealthBarBorderTexture.width,
										HealthBarBorderTexture.height));
			//Health Bar Background
			if (displayBackGround)
			GUI.DrawTexture(new Rect(	HealthBarBorderSizeLeft,
										HealthBarBorderSizeUp,
										(HealthBarSize.x-HealthBarBorderSizeLeft-HealthBarBorderSizeRight),
										HealthBarSize.y-HealthBarBorderSizeDown-HealthBarBorderSizeUp),
								HealthBarBackground,
								ScaleMode.StretchToFill);
			
			//Health Bar 
			if (displayHealthBar)
			GUI.DrawTexture(new Rect(	HealthBarBorderSizeLeft,
										HealthBarBorderSizeUp+paddingUp,
										(HealthBarSize.x-HealthBarBorderSizeLeft-HealthBarBorderSizeRight)*((Health/maxHealth)),
										HealthBarSize.y-HealthBarBorderSizeDown-HealthBarBorderSizeUp-paddingUp-paddingDown),
								HealthBarTexture,
								ScaleMode.ScaleAndCrop);
			
			if (displayDividers){
			//float divideWidth = 20;
				float divideWidth = healthSquareBorderTexture.width;
			for (int i = 0; i <= maxHealth/healthSquareValue; i++) {
				
				Vector2 startPosition;
				//healthSquareBorder.width = 5;
				float healthSectionWidth = (HealthBarSize.x-HealthBarBorderSizeLeft-HealthBarBorderSizeRight)/(maxHealth/healthSquareValue);
				//healthSectionWidth -= divideWidth/2;
				startPosition.x = HealthBarBorderSizeLeft-divideWidth/2 + i*(healthSectionWidth);
				
				
					GUI.DrawTexture(new Rect(	startPosition.x,
												HealthBarBorderSizeUp,
												divideWidth,//(HealthBarSize.x-HealthBarBorderSizeLeft-HealthBarBorderSizeRight)*((Health/maxHealth)),
												HealthBarSize.y-HealthBarBorderSizeDown-HealthBarBorderSizeUp),
								healthSquareBorderTexture,
								ScaleMode.StretchToFill);
				
				
			}
			GUI.DrawTexture(new Rect(	(HealthBarSize.x-HealthBarBorderSizeLeft-HealthBarBorderSizeRight)*((Health/maxHealth))+HealthBarBorderSizeLeft-divideWidth/2,
												HealthBarBorderSizeUp,
												divideWidth,//(HealthBarSize.x-HealthBarBorderSizeLeft-HealthBarBorderSizeRight)*((Health/maxHealth)),
												HealthBarSize.y-HealthBarBorderSizeDown-HealthBarBorderSizeUp),
								healthSquareBorderTexture,
								ScaleMode.StretchToFill);
			}

			// Health Bar Border
			if (displayBorder){
			GUI.DrawTexture(new Rect(	0,
										0,
										HealthBarBorderTexture.width,
										HealthBarBorderTexture.height),
								HealthBarBorderTexture,
								ScaleMode.ScaleAndCrop);
			}
				
		

			GUI.EndGroup();
		}

		if (debug){
			if (GUI.Button(new Rect(Screen.width/2-100,Screen.height-175,100,25),"+10 Health")){
				if (Health < maxHealth)
					Health += 10; 	
			}
			if (GUI.Button(new Rect(Screen.width/2-100,Screen.height-200,100,25),"-10 Health")){
				if (Health > 1) 
					Health -= 10; 	
			}
			if (GUI.Button(new Rect(Screen.width/2-100,Screen.height-225,100,25),"+10 MaxHealth")){
					maxHealth += 10; 	
					Health += 10;
			}
			if (GUI.Button(new Rect(Screen.width/2-100,Screen.height-250,100,25),"-10 Max Health")){
					maxHealth = Mathf.Max(maxHealth-10,0);
					Health = Mathf.Min(maxHealth,Health);
			}
			displayBackGround	= GUI.Toggle(new Rect(Screen.width/2-100,Screen.height-75,150,25),displayBackGround,"Display Background");
			displayBorder		= GUI.Toggle(new Rect(Screen.width/2-100,Screen.height-100,150,25),displayBorder,"Display Border");
			displayDividers		= GUI.Toggle(new Rect(Screen.width/2-100,Screen.height-125,150,25),displayDividers,"Display Dividers");
			displayHealthBar	= GUI.Toggle(new Rect(Screen.width/2-100,Screen.height-150,150,25),displayHealthBar,"Display Healthbar");
			GUI.Label(new Rect(Screen.width/2-100,Screen.height-50,100,25),"HP : " + Health.ToString() + "/" + maxHealth.ToString(),"box");
		}
		}
	}
	

	public void setHealth(float HP) {
		Health = HP;
	}
	
	public float getHealth(){
		return Health;	
	}
	
	public void setMaxHealth(float HP){
		maxHealth = HP;	
	}
	
		public void setDisplay(bool Display){
		display = Display;	
	}
}